﻿// JScript 文件
//是否
function CloseWindow(value) {
    try {
        if (value)
            window.parent.document.getElementById("changed").value = 'true';
        else
            window.parent.document.getElementById("changed").value = 'false';
    } catch (e) {
        alert(e.ErrorMessage);
    }

    return true;
}

//保存数据
function SaveAll() {
    try {
        if (window.confirm('数据已修改,确认保存?')) {
            document.frames["FormView"].SaveForm();
        }
    }
    catch (e) {
        //alert(e.ErrorMessage);
    }
}

//打开新窗口
//url:路径
//width:宽度
//height:高度
function OpenWindow(url, width, height) {
    var left = (window.screen.width - width) / 2;
    var top = (window.screen.height - height) / 2;
    var parmars = 'top=' + top + ',left=' + left + ',toolbar=no,menubar=no,scrollbars=no,location=no,status=no,resizable=no,width=' + width + ',height=' + height;
    window.open(url, '', parmars);
}

//打开模态窗口
//url:路径
//width:宽度
//height:高度
function OpenModelWindow(url, width, height) {
    var left = (window.screen.width - width) / 2;
    var top = (window.screen.height - height) / 2;
    var parmars = 'dialogTop=' + top + 'px;dialogLeft=' + left + 'px;center=yes;resizable=no;dialogHeight=' + height + 'px;dialogWidth=' + width + 'px;status=no;';
    var returnValue = false;
    returnValue = window.showModalDialog(url, null, parmars);

    return returnValue;
}
//禁用页面控件
///如果projectType=state则禁用所有控件
///
function ForbidControl(projectType, state) {
    var elements = window.document.forms[0].elements;
    if (projectType == state) {
        for (var i = 0; i < elements.length; i++) {
            switch (elements[i].type) {
                case "textarea":
                case "text":
                    {
                        elements[i].readOnly = true;
                        elements[i].onfocus = function() { };
                    }; break;
                case "submit":
                case "button":
                case "file":
                case "checkbox":
                case "radio":
                case "reset":
                    {
                        elements[i].disabled = true;
                        elements[i].onfocus = function() { };
                    }; break;
            }
        }
    }
}

//禁用页面控件
///如果projectType=state则禁用所有控件
///ArrayControl里的控件不禁用
function ForbidControls(projectType, state, ArrayControl) {
    var elements = window.document.forms[0].elements;

    if (projectType == state) {
        for (var i = 0; i < elements.length; i++) {
            if (ArrayControl.length != 0) {
                for (var j = 0; j < ArrayControl.length; j++) {
                    if (elements[i].id != ArrayControl[j]) {
                        switch (elements[i].type) {
                            case "textarea":
                            case "text":
                                {
                                    elements[i].readOnly = true;
                                    elements[i].onfocus = function() { };
                                }; break;
                            case "submit":
                            case "button":
                            case "file":
                            case "checkbox":
                            case "radio":
                            case "reset":
                                {
                                    elements[i].disabled = true;
                                    elements[i].onfocus = function() { };
                                }; break;
                        }
                    }
                }
            }
            else {
                switch (elements[i].type) {
                    case "textarea":
                    case "text":
                        {
                            elements[i].readOnly = true;
                            elements[i].onfocus = function() { };
                        }; break;
                    case "submit":
                    case "button":
                    case "checkbox":
                    case "radio":
                    case "reset":
                    case "file":
                        {
                            elements[i].disabled = true;
                            elements[i].onfocus = function() { };
                        }; break;
                }
            }
        }
    }
}

///给控件添加改变事件
///
///
function IsChange() {
    var elements = window.document.forms[0].elements;
    for (var i = 0; i < elements.length; i++) {
        switch (elements[i].type) {
            case "textarea":
            case "checkbox":
            case "radio":
            case "text":
            case "file":
                {
                    elements[i].attachEvent("onchange", function() {
                        try {
                            window.parent.isChange = true;
                        }
                        catch (e) {
                        }
                    });
                }; break
        }
    }
    var select = document.getElementById("TxtdropTdyt");
    if (select != null) {
        select.attachEvent("onchange", function() {
            try {
                window.parent.isChange = true;
            }
            catch (e) {
            }
        });
    }

}

function IsChangeTwo() {
    var elements = window.document.forms[0].elements;
    for (var i = 0; i < elements.length; i++) {
        switch (elements[i].type) {
            case "textarea":
            case "checkbox":
            case "radio":
            case "text":
            case "file":
                {
                    elements[i].attachEvent("onchange", function() {
                        try {
                            window.parent.parent.isChange = true;
                        }
                        catch (e) {
                        }
                    });
                }; break
        }
    }

    var text = document.getElementsByTagName("input");
    for (var i = 0; i < text.length; i++) {
        if (text[i].type == "text") {
            text[i].attachEvent("onfocus", function() {
                try {
                    window.parent.parent.isChange = true;
                }
                catch (e) {
                }
            });
        }
    }
}

///打印时隐藏窗体的button按钮
function HiddenButton() {
    var elements = window.document.forms[0].elements;
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == "submit" || elements[i].type == "button" || elements[i].type == "checkbox" || elements[i].type == "reset") {
            elements[i].style.display = "none";
        }

        if (elements[i].type == "text") {
            elements[i].className = "text";
            elements[i].disabled = false;
        }

        if (elements[i].type == "textarea") {
            elements[i].className = "textarea";
            elements[i].disabled = false;
        }
    }

    try {
        document.getElementById("noPrint").style.display = "none";
    }
    catch (e) {
    }
}

function PrintWindow(url) {
}

function onloadFunction() {
    calendarOnUpdateFrom = function(cal, dateStr) {
        document.getElementById("timeFromOfAccept").value = (dateStr);
    }
    calendarOnUpdateTo = function(cal, dateStr) {
        document.getElementById("timeToOfAccept").value = (dateStr);
    }
    //日历
    Calendar.setup({
        button: "fromImg",  // trigger for the calendar (button ID)
        eventName: 'click',
        //inputField : "CFSJTo",
        ifFormat: "%Y-%m-%d",    // if = input field如果没有输入框本设置无效
        daFormat: "%Y-%m-%d",    //da = displayArea
        showsTime: true,   // will display a time selector是否显示时间（时，分，秒）
        singleClick: true,   // double-click mode不太清楚
        onSelect: calendarOnUpdateFrom, //当选定后会触发本方法
        step: 1   // show all years in drop-down boxes (instead of every other year as default)
    });

    Calendar.setup({
        button: "toImg",  // trigger for the calendar (button ID)
        eventName: 'click',
        //inputField : "CFSJFrom",
        ifFormat: "%Y-%m-%d",    // if = input field如果没有输入框本设置无效
        daFormat: "%Y-%m-%d",    //da = displayArea
        showsTime: true,   // will display a time selector是否显示时间（时，分，秒）
        singleClick: true,   // double-click mode不太清楚
        onSelect: calendarOnUpdateTo, //当选定后会触发本方法
        step: 1   // show all years in drop-down boxes (instead of every other year as default)
    });
    

};


if (!window["yn"]) yn = {};
yn.NativeWindow = function() {
    this.config = null;
    this.params = {};
};
yn.NativeWindow.prototype = {
    handler: null,
    config: null,
    params: null,
    url: null,
    setUrl: function(url) {
        this.url = url;
    },
    open: function() {
        var width = 400;
        var height = 400;
        var config = this.config;
        if (config) {
            width = config.width;
            height = config.height;
        }
        var left = (screen.width - width) / 2;
        var top = (screen.height - height) / 2;
        var url = this.url;
        var params = this.params;
        if (params != null) {
            if (url.indexOf("?") < 0) {
                url += "?";
            }
            var j = 0;
            for (var key in params) {
                if (j != 0) url += "&";
                url += key + "=" + params[key];
            }
        }
        this.handler = window.open(url, '_blank', 'height=' + height + ', width=' + width + ', top=' + top + ', left=' + left + ', toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=no');
    },
    set: function(pros) {
        for (var key in pros) {
            this.config[key] = pros[key];
        }
    },
    setParames: function(params) {
        for (var key in params) {
            this.params[key] = params[key];
        }
    }
};
var editorWindow;
function showEditorWindow(aID, act, ev) {
    var markId = 'userEditor';
    //alert(event);
    //var userEditor = punishService.getuserEditor(userID);
    if (!editorWindow) {
        var iframeStr = "<iframe id='userEditorIframe' src='about:blank' width='100%' height='100%' border='0'></iframe>";
        var w = 500;
        var h = 500;
        var l = 0;
        var t = 0;
        var tit = "添加签名图片";
        var msg = iframeStr;
        editorWindow = new xWin(markId, w, h, l, t, tit, msg);
    }
    document.getElementById("xMsg" + markId).style.display = "block";
    document.getElementById("shadowDiv" + markId).style.display = "block";
    document.getElementById(markId + "Iframe").src = "UserEditor.aspx?userid=" + aID;

    caculatePosition(document.getElementById("xMsg" + markId), ev);
    caculatePosition(document.getElementById("shadowDiv" + markId), ev);

}

function caculatePosition(popWindow, ev) {
    //var event = window.event;
    var rightedge = document.body.clientWidth - ev.clientX;
    var bottomedge = document.body.clientHeight - ev.clientY;

    var l = 0, t = 0;
    if (rightedge < popWindow.offsetWidth)
        l = document.body.scrollLeft + ev.clientX - popWindow.offsetWidth;
    else
        l = document.body.scrollLeft + ev.clientX;

    if (bottomedge < popWindow.offsetHeight)
        t = document.body.scrollTop + ev.clientY - popWindow.offsetHeight;
    else
        t = document.body.scrollTop + ev.clientY;

    if (l < 0) l = 0;
    if (t < 0) t = 0;
    popWindow.style.left = l;
    popWindow.style.top = t;
}
function caculateTime(timeRemain, secondsPerDay) {

    if (timeRemain < 0) timeRemainStr = "-"
    else timeRemainStr = "";
    timeRemain = Math.abs(timeRemain);
    var days = Math.floor(timeRemain / (secondsPerDay));
    var hours = Math.floor((timeRemain % (secondsPerDay)) / 3600);
    var minutes = Math.floor((timeRemain % 3600) / 60);
    var seconds = timeRemain % 60;
    timeRemainStr += days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
    return timeRemainStr;
}

function openWorkFlowChooser() {
    var win = new yn.NativeWindow;
    win.setUrl("workflows2.aspx");
    win.open();
}
function openDepartmentChooser() {
    parent.frames["right"].location = "departments.aspx";
}
function openUserChooser() {
    var win = new yn.NativeWindow;
    win.setUrl("users.aspx");
    win.open();
}

function showAsChart() {
    document.forms["queryForm"].displayType.value = "charts";
    document.forms["queryForm"].submit();
    document.forms["queryForm"].displayType.value = "table";
}

function selectWorkflow() {
    var wname = document.getElementById("wname").value;
    parent.frames["right"].location = "step_show.aspx?WNAME=" + wname;
}

function checkFormForStepAssess() {
    var wname = document.forms["queryForm"].wname.value;
    var stepName = document.forms["queryForm"].stepName.value;
    var msg = "";
    var pass = true;
    if (wname == "") {
        msg += "需要选择流程。\n";
        pass = false;
    }
    if (stepName == "") {
        msg += "需要选择岗位。\n";
        pass = false;
    }
    if (!pass) {
        alert(msg);
        return false;
    }
    return true;
}

function checkFormForDepartmentAssess() {
    var wname = document.forms["queryForm"].wname.value;
    var orderID = document.forms["queryForm"].orderID.value;
    var msg = "";
    var pass = true;
    if (wname == "") {
        msg += "需要选择流程。\n";
        pass = false;
    }
    if (orderID == "") {
        msg += "需要选择部门。\n";
        pass = false;
    }
    if (!pass) {
        alert(msg);
        return false;
    }
    return true;

}